<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--父组件模板-->
<div id="app">
    <!--    监听事件-->
    <cpn @itemclick="cpnClick"></cpn>
</div>


<!--子组件模板-->
<template id="cpn">
    <div>
        <button v-for="item in categories" @click="btnClick(item)">
            {{item.name}}
        </button>
    </div>
</template>

<script src="../js/vue.js"></script>
<script>

    //子组件
    const cpn = {
        template: `#cpn`,
        data() {
            return {
                categories: [
                    {id: 'aaa', name: '热门推荐'},
                    {id: 'bbb', name: '手机推荐'},
                    {id: 'ccc', name: '电脑推荐'},
                    {id: 'ddd', name: '家电推荐'},
                ]
            }
        },
        methods: {
            btnClick(item) {
                //发射事件:自定义事件
                this.$emit('itemclick', item)
            }
        }
    }

    //父组件
    const app = new Vue({
        el: '#app',
        data: {
            message: '卢本伟牛逼！'
        },
        components: {
            cpn
        },
        methods: {
            //处理事件
            cpnClick(item) {
                console.log('cpn', item);
            }
        }
    })
</script>
</body>
</html>